package com.asr.testclient.service;

import com.asr.testclient.model.ServerMessage;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
 * 测试MessageParser类
 */
class MessageParserTest {
    
    private MessageParser messageParser;
    
    @BeforeEach
    void setUp() {
        messageParser = new MessageParser();
    }
    
    @Test
    void testParse_WithValidRecognitionResultMessage() {
        String json = "{\"type\":\"file_recognition_result\",\"filename\":\"test.wav\",\"text\":\"这是识别结果\",\"timestamp\":\"2025-11-03T10:30:00\"}";
        
        ServerMessage message = messageParser.parse(json);
        
        assertNotNull(message);
        assertEquals("file_recognition_result", message.getType());
        assertEquals("这是识别结果", message.getText());
        assertEquals("test.wav", message.getFilename());
        assertEquals("2025-11-03T10:30:00", message.getTimestamp());
    }
    
    @Test
    void testParse_WithValidErrorMessage() {
        String json = "{\"type\":\"error\",\"message\":\"无法识别音频文件\"}";
        
        ServerMessage message = messageParser.parse(json);
        
        assertNotNull(message);
        assertEquals("error", message.getType());
        assertEquals("无法识别音频文件", message.getMessage());
    }
    
    @Test
    void testParse_WithInvalidJson() {
        String invalidJson = "{invalid json format";
        
        ServerMessage message = messageParser.parse(invalidJson);
        
        assertNull(message);
    }
    
    @Test
    void testParse_WithEmptyJson() {
        String emptyJson = "";
        
        ServerMessage message = messageParser.parse(emptyJson);
        
        assertNull(message);
    }
    
    @Test
    void testParse_WithNullJson() {
        ServerMessage message = messageParser.parse(null);
        
        assertNull(message);
    }
    
    @Test
    void testParse_WithWhitespaceJson() {
        String whitespaceJson = "   ";
        
        ServerMessage message = messageParser.parse(whitespaceJson);
        
        assertNull(message);
    }
    
    @Test
    void testParse_WithJsonMissingTypeField() {
        String json = "{\"text\":\"some text\"}";
        
        ServerMessage message = messageParser.parse(json);
        
        // 应该能解析，但type字段为null
        assertNotNull(message);
        assertNull(message.getType());
    }
    
    @Test
    void testExtractRecognitionResult_WithValidMessage() {
        String json = "{\"type\":\"file_recognition_result\",\"text\":\"识别的文本内容\"}";
        ServerMessage message = messageParser.parse(json);
        
        String result = messageParser.extractRecognitionResult(message);
        
        assertNotNull(result);
        assertEquals("识别的文本内容", result);
    }
    
    @Test
    void testExtractRecognitionResult_WithWrongMessageType() {
        String json = "{\"type\":\"error\",\"message\":\"错误信息\"}";
        ServerMessage message = messageParser.parse(json);
        
        String result = messageParser.extractRecognitionResult(message);
        
        assertNull(result);
    }
    
    @Test
    void testExtractRecognitionResult_WithNullMessage() {
        String result = messageParser.extractRecognitionResult(null);
        
        assertNull(result);
    }
    
    @Test
    void testExtractRecognitionResult_WithEmptyTextField() {
        String json = "{\"type\":\"file_recognition_result\",\"text\":\"\"}";
        ServerMessage message = messageParser.parse(json);
        
        String result = messageParser.extractRecognitionResult(message);
        
        // 空文本应该返回null
        assertNull(result);
    }
    
    @Test
    void testExtractRecognitionResult_WithNullTextField() {
        String json = "{\"type\":\"file_recognition_result\"}";
        ServerMessage message = messageParser.parse(json);
        
        String result = messageParser.extractRecognitionResult(message);
        
        assertNull(result);
    }
    
    @Test
    void testExtractErrorMessage_WithValidErrorMessage() {
        String json = "{\"type\":\"error\",\"message\":\"文件格式不支持\"}";
        ServerMessage message = messageParser.parse(json);
        
        String errorMsg = messageParser.extractErrorMessage(message);
        
        assertNotNull(errorMsg);
        assertEquals("文件格式不支持", errorMsg);
    }
    
    @Test
    void testExtractErrorMessage_WithWrongMessageType() {
        String json = "{\"type\":\"file_recognition_result\",\"text\":\"识别结果\"}";
        ServerMessage message = messageParser.parse(json);
        
        String errorMsg = messageParser.extractErrorMessage(message);
        
        assertNull(errorMsg);
    }
    
    @Test
    void testExtractErrorMessage_WithNullMessage() {
        String errorMsg = messageParser.extractErrorMessage(null);
        
        assertNull(errorMsg);
    }
    
    @Test
    void testExtractErrorMessage_WithEmptyMessageField() {
        String json = "{\"type\":\"error\",\"message\":\"\"}";
        ServerMessage message = messageParser.parse(json);
        
        String errorMsg = messageParser.extractErrorMessage(message);
        
        // 空消息应该返回null
        assertNull(errorMsg);
    }
    
    @Test
    void testExtractErrorMessage_WithNullMessageField() {
        String json = "{\"type\":\"error\"}";
        ServerMessage message = messageParser.parse(json);
        
        String errorMsg = messageParser.extractErrorMessage(message);
        
        assertNull(errorMsg);
    }
}
